//
//  StoryCell.m
//  MyItem
//
//  Created by SoulWater on 15/12/30.
//  Copyright © 2015年 admin. All rights reserved.
//

#import "StoryCell.h"

@implementation StoryCell

- (void)awakeFromNib {
    // Initialization code
}

-(void)setStoryModel:(StoryModel *)storyModel
{
    if (_storyModel != storyModel) {
        
        _storyModel = storyModel;
        
        [self setNeedsLayout];
    }
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    NSString *iconStr = self.storyModel.pubUserImg;
    NSString *newString = [iconStr substringToIndex:iconStr.length-5];

    [_iconImageView sd_setImageWithURL:[NSURL URLWithString:newString]];
    _iconImageView.layer.cornerRadius = 35;
    _iconImageView.layer.masksToBounds = YES;
    
    _userNameLabel.text = self.storyModel.pubUserName;
    
    _bodyLabel.text = self.storyModel.body;
    
    NSString *urlStr = self.storyModel.wareInfoList[0][@"imgURL"];
    NSString *newStr = [urlStr substringToIndex:urlStr.length-5];
    [_wareImgView sd_setImageWithURL:[NSURL URLWithString:newStr]];
    
    CommonCtrView *button1 = [[CommonCtrView alloc]init];
    [button1 _createPictureAndRightLabel:@"jd_myjd_ub_tab_favorite_n@2x" imageScale:0.9 labelText:[NSString stringWithFormat:@"%@",self.storyModel.praiseCnt]];
    [_buttonView1 addSubview:button1];
    
    CommonCtrView *button2 = [[CommonCtrView alloc]init];
    [button2 _createPictureAndRightLabel:@"jd_myjd_ub_tab_massage_n@2x" imageScale:0.9 labelText:[NSString stringWithFormat:@"%@",self.storyModel.commentCnt]];
    [_buttonView2 addSubview:button2];
    
    CommonCtrView *button3 = [[CommonCtrView alloc]init];
    [button3 _createPictureAndRightLabel:@"topbar_share_item@2x" imageScale:0.9 labelText:nil];
    [_buttonView3 addSubview:button3];
    
    
    if (self.storyModel.userInfoList.count > 0) {
        NSMutableArray *userArray = [NSMutableArray array];
        for (NSDictionary *dic in self.storyModel.userInfoList) {
            
            NSString *imgURL = dic[@"imgURL"];
            
            [userArray addObject:imgURL];
        }
        
        NSArray *imgViewArray = @[_userInfoImgView1,_userInfoImgView2,_userInfoImgView3,_userInfoImgView4,_userInfoImgView5,_userInfoImgView6,_userInfoImgView7,_userInfoImgView8];
        NSInteger count = 0;
        if (self.storyModel.userInfoList.count >=8) {
            
            count = 8;
        }
        else
        {
            count = self.storyModel.userInfoList.count;
        }
        
        for (int i = 0; i < count; i++) {

            NSString *urlStr = userArray[i];
            NSString *newStr;
            if (![urlStr isEqualToString:@"http://go.m.jd.com/img/no-img.png"]) {
                
                newStr = [urlStr substringToIndex:urlStr.length-5];
            }
            else{
                newStr = @"http://go.m.jd.com/img/no-img.png";
            }
            [imgViewArray[i] sd_setImageWithURL:[NSURL URLWithString:newStr]];
            
            UIImageView *imgView = [self.contentView viewWithTag:100+i];
            imgView.layer.cornerRadius = 20;
            imgView.layer.masksToBounds = YES;
            imgView.hidden = NO;
          
        }
        
        if (count < 8) {
            
            for (int i = 0; i < 8- count; i++) {
                
                UIImageView *imgView = [self.contentView viewWithTag:100+count+i];
                
                imgView.hidden = YES;
            }
        }
    }
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
